package com.xiaoshuidi.cloud.module.member.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoshuidi.cloud.framework.common.exception.ServiceException;
import com.xiaoshuidi.cloud.framework.common.exception.enums.GlobalErrorCodeConstants;
import com.xiaoshuidi.cloud.framework.common.util.json.JsonUtils;
import com.xiaoshuidi.cloud.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.xiaoshuidi.cloud.framework.security.core.util.SecurityFrameworkUtils;
import com.xiaoshuidi.cloud.module.member.controller.admin.activities.vo.ActivitiesCreateVO;
import com.xiaoshuidi.cloud.module.member.controller.admin.activities.vo.ActivityPageVO;
import com.xiaoshuidi.cloud.module.member.controller.app.activities.vo.EnrollListVO;
import com.xiaoshuidi.cloud.module.member.controller.app.activities.vo.EnrollStatusPageVO;
import com.xiaoshuidi.cloud.module.member.enums.ActivitiesStatusEnum;
import com.xiaoshuidi.cloud.module.member.enums.StatusEnum;
import com.xiaoshuidi.cloud.module.member.mapper.activities.MemberActivitiesMapper;
import com.xiaoshuidi.cloud.module.member.pojo.activities.MemberActivities;
import com.xiaoshuidi.cloud.module.member.pojo.programarticle.MemberProgramInfo;
import com.xiaoshuidi.cloud.module.member.pojo.topics.MemberTopics;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Repository
@RequiredArgsConstructor
public class MemberActivitiesRepository extends ServiceImpl<MemberActivitiesMapper, MemberActivities> {
    private final MemberActivitiesMapper mapper;

    public Boolean create(ActivitiesCreateVO vo) {
        //检验是否存在相同活动名开始时间结束时间相同
        LambdaQueryWrapper<MemberActivities> wrapper = new LambdaQueryWrapper<MemberActivities>()
                .eq(MemberActivities::getName, vo.getName())
                .eq(MemberActivities::getCreateTime, vo.getBeginTime())
                .eq(MemberActivities::getEndTime, vo.getEndTime());
        List<MemberActivities> list = mapper.selectList(wrapper);
        if (list != null && list.size() > 0) {
            throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(), "该活动已创建!");
        }
        MemberActivities memberActivities = new MemberActivities();
        BeanUtils.copyProperties(vo, memberActivities);
        if (vo.getFillItemList() != null && vo.getFillItemList().size() > 0) {
            memberActivities.setFillItem(JsonUtils.toJsonString(vo.getFillItemList()));
        }
        mapper.insert(memberActivities);
        return true;
    }


    public List<MemberActivities> getList() {
        //查询到当前时间在活动起止时间内
        LambdaQueryWrapper<MemberActivities> wrapper = new LambdaQueryWrapperX<MemberActivities>()
              //  .ge(MemberActivities::getEndTime, LocalDateTime.now())
                .orderByDesc(MemberActivities::getCreateTime);
        return mapper.selectList(wrapper);
    }

    public Boolean enrollOrNot(Long activityId) {
        //是否在报名时间
        LambdaQueryWrapper<MemberActivities> wrapper = new LambdaQueryWrapper<MemberActivities>()
                .le(MemberActivities::getEnrollBegin, LocalDateTime.now())
                .ge(MemberActivities::getEnrollEnd, LocalDateTime.now())
                .eq(MemberActivities::getId, activityId);
        List<MemberActivities> list = mapper.selectList(wrapper);
        if (list != null && list.size() > 0) {
            return true;
        }
        return false;
    }

    public IPage<EnrollListVO> getStatusPage(EnrollStatusPageVO page) {
        Long id = SecurityFrameworkUtils.getLoginUser().getId();
        page.setUserId(id);
        IPage<EnrollListVO> result = mapper.getStatusPage(new Page<MemberActivities>(page.getPageNo(), page.getPageSize()), page);
        return result;
    }

    public IPage<EnrollListVO> getPage(EnrollStatusPageVO page) {
        return mapper.getPage(new Page<MemberActivities>(page.getPageNo(), page.getPageSize()), page);
    }

    public IPage<MemberActivities> getActiveiesPage(ActivityPageVO pageVO, LocalDateTime now) {

        LambdaQueryWrapper<MemberActivities> wrapper = new LambdaQueryWrapperX<MemberActivities>()
                .likeIfPresent(MemberActivities::getName, pageVO.getName())
                .geIfPresent(MemberActivities::getCreateTime, pageVO.getBeginTime())
                .leIfPresent(MemberActivities::getCreateTime, pageVO.getEndTime())
                //待生效,活动开始时间>当前时间
                .gt(ObjectUtils.isNotEmpty(pageVO.getStatus()) && pageVO.getStatus().intValue() == ActivitiesStatusEnum.TO_BE_EFFECTIVE.getCode().intValue(),
                        MemberActivities::getBeginTime, now)
                //进行中 活动时间<=当前时间 活动结束时间>=当前时间
                .le(ObjectUtils.isNotEmpty(pageVO.getStatus()) && pageVO.getStatus().intValue() == ActivitiesStatusEnum.IN_PROGRESS.getCode().intValue(),
                        MemberActivities::getBeginTime, now)
                .ge(ObjectUtils.isNotEmpty(pageVO.getStatus()) && pageVO.getStatus().intValue() == ActivitiesStatusEnum.IN_PROGRESS.getCode().intValue(),
                        MemberActivities::getEndTime, now)
                //已结束 活动结束时间<当前时间
                .lt(ObjectUtils.isNotEmpty(pageVO.getStatus()) && pageVO.getStatus().intValue() == ActivitiesStatusEnum.ENDED.getCode().intValue(),
                        MemberActivities::getEndTime, now)
                .orderByDesc(MemberActivities::getWeight)
                .orderByDesc(MemberActivities::getCreateTime);
        return page(new Page<MemberActivities>(pageVO.getPageNo(), pageVO.getPageSize()), wrapper);
    }

    public Boolean edit(ActivitiesCreateVO vo) {
        MemberActivities memberActivities = new MemberActivities();
        BeanUtils.copyProperties(vo, memberActivities);
        if (vo.getFillItemList() != null && vo.getFillItemList().size() > 0) {
            memberActivities.setFillItem(JsonUtils.toJsonString(vo.getFillItemList()));
        }
        mapper.updateById(memberActivities);
        return true;
    }

    public List<MemberActivities> selectTobeValidList(LocalDateTime time) {
        //查询结束时间小于等于time的
        LambdaQueryWrapper<MemberActivities> wrapper = new LambdaQueryWrapper<MemberActivities>()
                .le(MemberActivities::getEndTime, time);
        return mapper.selectList(wrapper);
    }

    public Integer getActiviesStatus(MemberActivities t) {
        LocalDateTime now = LocalDateTime.now();
        //未生效 生效时间>当前时间
        if (t.getBeginTime().isAfter(now)) {
            return StatusEnum.NOT_EFFECTIVE.getCode();
        } else if ((t.getEndTime().isAfter(now) && now.isAfter(t.getBeginTime())) || t.getBeginTime() == now) {
            //已生效 生效时间<=当前时间<过期时间
            return StatusEnum.EFFECTIVE.getCode();
        } else {
            return StatusEnum.EXPIRED.getCode();
        }
    }
}
